<?php
require 'config.php';
require CAMINHO_SERVIDOR . 'funcoes.php';
require 'login.class.php';
$login = new Login();


if ($login->verificar(URL . 'index.php')) {
    $query = "SELECT * FROM tb_time WHERE status='S' ORDER BY nome";
    $times = $login->executaQueryAll($query);
    $query = "SELECT * FROM tb_campeonato WHERE status='S' ORDER BY descricao";
    $campeonatos = $login->executaQueryAll($query);
    if (!empty($_GET["id_jogo"])) {
        $query = "SELECT id_jogo, DATE_FORMAT(`data`,'%d/%m/%Y') AS `data`, 
                    DATE_FORMAT(`hora`,'%H:%i') AS `hora`, id_time_A,id_time_B, descricao,id_campeonato,id_rodada, `local`
                    FROM bolao.`tb_jogos` WHERE id_jogo = " . $_GET["id_jogo"];
        $dadosJogo = $login->executaQueryRow($query);
        //funcoes::debuga($dadosJogo);
    }

    $login->fecharConexao();
}
?>
<html>
    <head>
        <title>Jogo</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/classes.css">

    </head>
    <body>
        <div class="container">
            <div class="container-fluid margin10px">
                <a class="btn btn-info offset0" href="<?php echo URL_ADMIN; ?>" >Início</a>
            </div>
            <?php
            if (isset($_GET['msg'])) {
                if ($_GET['msg'] == 1) {
                    ?> 
                    <div class="col-md-8 col-md-offset-2">
                        <div class="alert alert-success text-center">Jogo cadastrado com sucesso!</div>
                    </div>
                    <?php
                } elseif ($_GET['msg'] == 2) {
                    ?> 
                    <div class="col-md-8 col-md-offset-2">
                        <div class="alert alert-success text-center">Jogo alterado com sucesso!</div>
                    </div>
                    <?php
                } else {
                    ?> 
                    <div class="col-md-8 col-md-offset-2">
                        <div class="alert alert-danger text-center">Ocorreu algum erro!</div>
                    </div>
                    <?php
                }
            }
            ?>
            <form id="cad_jogo" name="cad_jogo" action="<?php echo URL ?>criaJogo.php" method="POST">
                <input id="id_jogo" type="hidden" value="<?php
                if (isset($_GET["id_jogo"])) {
                    echo $_GET["id_jogo"];
                }
                ?>" name="id_jogo"/>
                <div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <h3 class="panel-title text-center">Cadastrar Jogos</h3>
                        </div>
                        <div class="panel-body">
                            <div class="col-md-12 col-md-offset-0">
                                <div class="form-group">
                                    <label for="descricao">Descrição</label>
                                    <input class="form-control" required placeholder="Descrição" name="descricao" id="descricao" value="<?php
                                    if (isset($dadosJogo["descricao"])) {
                                        echo $dadosJogo["descricao"];
                                    }
                                    ?>" />
                                </div>
                            </div>
                            <div class="col-md-6 col-md-offset-0">
                                <div class="form-group">
                                    <label for="campeonato">Campeonato</label>
                                    <select class="form-control" name="id_campeonato" onchange="carregaRodadas();" id="id_campeonato">
                                        <?php
                                        foreach ($campeonatos as $cam) {
                                            if ($cam["id_campeonato"] == $dadosJogo["id_campeonato"]) {
                                                $selected = "selected='selected'";
                                            } else {
                                                $selected = "";
                                            }
                                            ?>
                                            <option <?php echo $selected ?>  value="<?php echo $cam["id_campeonato"]; ?>"><?php echo $cam["descricao"]; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6 col-md-offset-0">
                                <div class="form-group">
                                    <label for="rodada">Rodada</label>
                                    <select class="form-control" name="id_rodada" id="id_rodada">
                                        <option value="0">Escolha o campeonato ...</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6 col-md-offset-0">
                                <div class="form-group">
                                    <label for="time a">Time A (casa)</label>
                                    <select class="form-control" name="id_time_A" id="id_time_A">
                                        <?php
                                        foreach ($times as $time) {
                                            if ($time["id_time"] == $dadosJogo["id_time_A"]) {
                                                $selected = "selected='selected'";
                                            } else {
                                                $selected = "";
                                            }
                                            ?>
                                            <option <?php echo $selected ?> value="<?php echo $time["id_time"]; ?>"><?php echo $time["nome"]; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6 col-md-offset-0">
                                <div class="form-group">
                                    <label for="time b">Time B (visitante)</label>
                                    <select class="form-control" name="id_time_B" id="id_time_B">
                                        <?php
                                        foreach ($times as $time) {
                                            if ($time["id_time"] == $dadosJogo["id_time_B"]) {
                                                $selected = "selected='selected'";
                                            } else {
                                                $selected = "";
                                            }
                                            ?>
                                            <option <?php echo $selected ?> value="<?php echo $time["id_time"]; ?>"><?php echo $time["nome"]; ?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-4 col-md-offset-0">
                                <div class="form-group">
                                    <label for="data jogo">Data Jogo</label>
                                    <input class="form-control" required placeholder="20/02/2015" name="data" id="data" value="<?php
                                    if (isset($dadosJogo["data"])) {
                                        echo $dadosJogo["data"];
                                    }
                                    ?>" />
                                </div>
                            </div>
                            <div class="col-md-4 col-md-offset-0">
                                <div class="form-group">
                                    <label for="hora jogo">Hora Jogo</label>
                                    <input class="form-control" required placeholder="16:00" name="hora" id="hora" value="<?php
                                    if (isset($dadosJogo["hora"])) {
                                        echo substr($dadosJogo["hora"], 0, 5);
                                    }
                                    ?>" />
                                </div>
                            </div>
                            <div class="col-md-4 col-md-offset-0">
                                <div class="form-group">
                                    <label for="local">Local</label>
                                    <input class="form-control" required placeholder="Mineirão" name="local" id="local" value="<?php
                                    if (isset($dadosJogo["local"])) {
                                        echo $dadosJogo["local"];
                                    }
                                    ?>" />
                                </div>
                            </div>
                            <?php
                            if (isset($_GET["id_jogo"])) {
                                $texto_botao = "Alterar Jogo";
                            } else {
                                $texto_botao = "Cadastrar Jogo";
                            }
                            ?>
                            <input class="btn btn-info col-md-4 col-md-offset-1" id="bt_enviar" type="submit" value="<?php echo $texto_botao; ?>"></input>
                            <a class="btn btn-info col-md-4  col-md-offset-2" href="<?php echo URL; ?>listaJogos.php" >Listar Jogos</a>
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <script src="js/jquery-1.8.3.js"></script>
        <script src="js/jquery.validate.js"></script>
        <script type="text/javascript">
                                        function carregaRodadas() {
                                            var data = "id_campeonato=" + $('#id_campeonato').val();
                                            $.ajax({
                                                type: "POST",
                                                dataType: "json",
                                                url: "carregaRodadas.php", //Relative or absolute path to response.php file
                                                data: data,
                                                success: function (data) {
                                                    document.getElementById("id_rodada").options.length = 0;
                                                    var select = document.getElementById("id_rodada");
                                                    $.each(data, function (index, value) {
                                                        var el = document.createElement("option");
                                                        el.textContent = value.descricao;
                                                        el.value = value.id_rodada;
                                                        select.appendChild(el);

                                                        console.log('My array has at position ' + index + ', this value: ' + value.descricao);
                                                    });
                                                    if (document.getElementById("id_rodada").options.length === 0) {
                                                        var el = document.createElement("option");
                                                        el.textContent = "Nenhuma rodada cadastrada";
                                                        el.value = 0;
                                                        select.appendChild(el);
                                                    }
                                                }
                                            });
                                        }
                                        function desabilita() {
                                            jQuery('#bt_enviar').attr("value", "Aguarde...");
                                            jQuery('#bt_enviar').attr("disabled", "disabled");
                                        }
        </script>
        <script type="text/javascript">
            jQuery(document).ready(function () {
                jQuery('#cad_jogo').validate({
                    rules: {
                        descricao: {
                            required: true
                        },
                        local: {
                            required: true
                        },
                        data: {
                            required: true
                        },
                        hora: {
                            required: true
                        }
                    },
                    messages: {
                        descricao: {
                            required: "campo obrigatório."
                        },
                        local: {
                            required: "campo obrigatório."
                        },
                        data: {
                            required: "campo obrigatório."
                        },
                        hora: {
                            required: "campo obrigatório."
                        }
                    },
                    submitHandler: function (form) {
                        desabilita();
                        jQuery('#cad_jogo').submit();
                    }
                });
                carregaRodadas();
            });
        </script>
    </body>
</html>